package com.getui.java.pushmessage;

import java.util.ArrayList;
import java.util.List;

import com.getui.java.COMM;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.uitls.AppConditions;
import com.gexin.rp.sdk.base.uitls.AppConditions.OptType;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;

public class PushtoAPP {

	public static void main(String[] args) throws Exception {

		IGtPush push = new IGtPush(COMM.host, COMM.appKey, COMM.masterSecret);

		LinkTemplate template = linkTemplateDemo();
		AppMessage message = new AppMessage();
		message.setData(template);

		message.setOffline(true);
		// 离线有效时间，单位为毫秒，可选
		message.setOfflineExpireTime(24 * 1000 * 3600);
		// 推送给App的目标用户需要满足的条件
		AppConditions cdt = new AppConditions();
		List<String> appIdList = new ArrayList<String>();
		appIdList.add(COMM.appId);
		message.setAppIdList(appIdList);
		// 手机类型
		List<String> phoneTypeList = new ArrayList<String>();
		phoneTypeList.add("ANDROID");
		phoneTypeList.add("IOS");
		// 省份
		List<String> provinceList = new ArrayList<String>();
		provinceList.add("35000000");
		// 自定义tag
		List<String> tagList = new ArrayList<String>();

		cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList, OptType.or);
		cdt.addCondition(AppConditions.REGION, provinceList);
		cdt.addCondition(AppConditions.TAG, tagList);
		message.setConditions(cdt);

		IPushResult ret = push.pushMessageToApp(message, "任务别名_toApp");
		System.out.println(ret.getResponse().toString());
		// {result=ok, contentId=OSA-1208_MNhxaq3bV7ANGHXP6kBD1}
		if ("ok".equals(ret.getResponse().get("result"))) {
			System.out.println("发送成功");
		} else {
			System.out.println("发送失败");
		}
	}

	public static LinkTemplate linkTemplateDemo() throws Exception {
		LinkTemplate template = new LinkTemplate();
		template.setAppId(COMM.appId);
		template.setAppkey(COMM.appKey);
		template.setTitle("请输入通知栏标题");
		template.setText("请输入通知栏内容");
		template.setLogo("icon.png");
		template.setLogoUrl("");
		template.setIsRing(true);
		template.setIsVibrate(true);
		template.setIsClearable(true);
		template.setUrl("http://www.baidu.com");

		return template;
	}

}